home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 September / Australian PC User - September 2003 (CD1).iso / magstuff / web / files / dwmx61.exe / Disk1 / data1.cab / Configuration_En / Commands / Optimize Image in Fireworks.js < prev    next >
Encoding:
JavaScript  |  2002-11-25  |  4.1 KB  |  136 lines

  1. // Copyright 1998, 2000-2002 Macromedia, Inc. All rights reserved.
  2. //
  3. // Command: Optimize Image in Fireworks
  4. //
  5. // This formless command attempts to find and launch 
  6. // a Fireworks optimization session on a selected image
  7. // within dw.
  8. //
  9. // Most of the work to do this is implemented in the
  10. // extension DLL FWLaunch.DLL.
  11. //
  12. // **************** Commands API *****************
  13.     
  14. function canAcceptCommand(){
  15.   var retVal = false;
  16.   // First validate that there's a document and the FWLaunch.DLL 
  17.   // has been loaded
  18.   if (dw.getDocumentDOM() && typeof( FWLaunch ) != "undefined"){
  19.     // Don't bother to check if a valid version of Fireworks is installed, 
  20.       // because we'll alert the user later about where they can download
  21.       // Fireworks for a free trial if they don't have it installed. 
  22.     // return TRUE if an image is selected
  23.     var node = dw.getDocumentDOM().getSelectedNode();
  24.     if (node != null && node.nodeType == Node.ELEMENT && node.tagName  == "IMG"  && node.getAttribute( "src" )){
  25.       retVal = true;
  26.     }
  27.   }
  28.   return retVal;
  29. }
  30.    
  31. //---------------    LOCAL FUNCTIONS   ---------------
  32.  
  33.    function optimizeImage()
  34.    {
  35.       var dom = dw.getDocumentDOM();
  36.  
  37.          // If they don't have Fireworks 2 or greater installed, give them 
  38.       // the link to the Macromedia site so they can get a free trial version.
  39.       if ( !FWLaunch.validateFireworks(2.0) )
  40.       {
  41.           alert( MSG_Err_FireworksNotInstalled );
  42.         return;
  43.       }
  44.       
  45.       // First check to see if we may launch a session; this
  46.       // currently always returns TRUE for windows, but may
  47.       // return FALSE on the Mac if there's already an 
  48.       // optimization session in progress
  49.       //
  50.       if ( !FWLaunch.mayLaunchFireworks() )
  51.       {
  52.          alert( MSG_Err_MayNotLaunch );
  53.          return;
  54.       }
  55.    
  56.       // Make sure the file has been saved to disk first so
  57.       // we know the document path (which the DLL uses, 
  58.       // among other things, to resolve doc-relative urls
  59.       // and determine working directory (on windows)).
  60.       //
  61.       if (dom.URL == "" ) {
  62.          if (confirm(MSG_Err_FileNotSaved) && dw.canSaveDocument(dom)) {
  63.            dw.saveDocument(dom);
  64.          }
  65.          if (dom.URL == "" )
  66.            return;
  67.          //otherwise file saved, so continue
  68.       }
  69.       // canAcceptCommand() should have validated that the 
  70.       // selection is an image node; go no further if we
  71.       // don't have a valid SRC attribute
  72.       //
  73.       var node      = dom.getSelectedNode();
  74.       var imageSrc  = node.getAttribute( "src" );
  75.       var width     = node.getAttribute( "width" );
  76.       var height    = node.getAttribute( "height" );
  77.  
  78.       if ( !imageSrc )
  79.       {
  80.          alert( MSG_Err_InvalidUsage );
  81.          return;
  82.       }
  83.       
  84.       // Force width and height to invalid state if they're
  85.       // not defined
  86.       //
  87.       if ( !width )
  88.          width = -1;
  89.          
  90.       if ( !height )
  91.          height = -1;
  92.       
  93.       // Fix up site relative URLs here...
  94.       //
  95.       var siteRoot = dw.getSiteRoot();
  96.       if ( siteRoot )
  97.       {
  98.          if ( imageSrc.indexOf( '/' ) == 0 )
  99.             imageSrc = siteRoot + imageSrc.substring( 1 );
  100.       }
  101.  
  102.       // Now invoke FWLaunch and process the return code
  103.       //
  104.       var rc = FWLaunch.optimizeInFireworks( unescape( dom.URL )
  105.                                            , unescape( imageSrc ) 
  106.                                            , width
  107.                                            , height );
  108.       switch( rc )
  109.       {
  110.          case 0:
  111.             break; // success!
  112.          
  113.          case 1:
  114.             alert( MSG_Err_InvalidUsage );
  115.             break;
  116.             
  117.          case 2:
  118.             alert( MSG_Err_ResponseFile );
  119.             break;
  120.             
  121.          case 3:
  122.             alert( MSG_Err_Dreamweaver );
  123.             break;
  124.          
  125.          case 4:
  126.             alert( MSG_Err_Fireworks );
  127.             break;
  128.             
  129.          default:
  130.             alert( MSG_Err_UnrecognizedRC + rc );
  131.             break;
  132.       }
  133.  
  134.       return;         
  135.    }
  136.